﻿namespace Mockingbird.MVC.Controllers
{
    using System;
    using System.Web.Mvc;
    using System.Web.Security;
    using Core.Infrastructure.Logging.Interfaces;
    using Core.Infrastructure.Membership.Identity.Implementation;
    using Core.Models;
    using Core.Repository;
    using Core.Utilities.Cryptography;
    using Infrastructure.BaseTypes;
    using Models;
    using Utilities.Session;

    public class AccountController : BaseController
    {
        private readonly IAccountRepository accountRepository;

        public AccountController(ILogger logger, IAccountRepository accountRepository)
            : base(logger)
        {
            this.accountRepository = accountRepository;
        }

        [AllowAnonymous]
        public ActionResult LogIn()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult LogIn(LoginModel model)
        {
            if (ModelState.IsValid)
            {

                if (accountRepository.ValidateUser(
                    model.Email,
                    Cryptography.GetMd5Hash(model.Password)))
                {
                    FormsAuthentication.SetAuthCookie(
                        model.Email,
                        true);
                    Session.Timeout = (int)FormsAuthentication.Timeout.TotalMinutes;

                    var user = accountRepository.GetUser(
                        model.Email);


                    Session[SessionConstants.User] = new CustomPrincipal
                    {
                        Properties = user
                    };
                }
                else
                {
                    ModelState.AddModelError("Email", "Login error");

                    return View(model);
                }

                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View(model);
            }
        }

        [AllowAnonymous]
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Register(UserPropertiesModel model)
        {
            if (ModelState.IsValid)
            {
                model.RegistryDate = DateTime.Now;
                model.Password = Cryptography.GetMd5Hash(model.Password);

                if(!accountRepository.RegisterUser(model))
                {
                    ModelState.AddModelError("Name", "User already exist");

                    return View(model);
                }

                return RedirectToAction("LogIn", "Account");
            }
            else
            {
                return View(model);
            }
        }

        [AllowAnonymous]
        public ActionResult LogOut()
        {
            Session.Abandon();
            FormsAuthentication.SignOut();

            return RedirectToAction("LogIn", "Account");
        }
    }
}
